package com.game.sprite.components;

import android.graphics.Canvas;

import com.game.utils.SynchronizeArrayList;


/**
 * 按钮组
* @author oven:
* @version 创建时间：2012-12-11 下午2:10:20
* 
*/
public class BasicButtonGroup {
	SynchronizeArrayList<BasicButton> buttons;
	int selectedIndex=-1;
	
	
	public  BasicButtonGroup() {
		buttons = new SynchronizeArrayList<BasicButton>();
	}
	
	/**
	 * 绘制按钮
	 * @param canvas
	 */
	public void draw(Canvas canvas){
		for(int i=0;i<buttons.size();i++){
			buttons.get(i).setIsPress(selectedIndex==i?true:false);
			buttons.get(i).draw(canvas);
		}
	}
	
	
	public void setButtons(SynchronizeArrayList<BasicButton> buttons){
		this.buttons = buttons;
	}
	
	public void addButton(BasicButton button){
		buttons.add(button);
	}
	
	public void removeButton(int index){
		buttons.remove(index);
	}
	
	public void removeButton(BasicButton button){
		buttons.remove(button);
	}
	
	
	/**
	 * 点击按钮
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean press(float x, float y) {
		boolean result = false;
		for(int i=0,n=buttons.size();i<n;i++){
			result = buttons.get(i).press(x, y);
			if(result){
				selectedIndex = i;
				result=true;
				break;
			}
		}
		return result;
	}

	/**
	 * 点击后释放并响应
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean responseClick(float x, float y) {
		if(selectedIndex!=-1){
			buttons.get(selectedIndex).release(x,y);
			selectedIndex=-1;
		}
		return false;
	}

	/**
	 * 释放点击
	 */
	public void releaseClick(){
		if(selectedIndex!=-1){
			selectedIndex=-1;
		}
	}
}

